package com.zhyl.demo.controller;

import com.zhyl.demo.entity.SendMessage;
import com.zhyl.demo.service.SendMessageService;
import com.zhyl.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * @author yang3
 */
@RestController
@RequestMapping("/message")
public class SendMessageController {
    private final SendMessageService sendMessageService;
    @Autowired
    private SendMessageController(SendMessageService sendMessageService)
    {
        this.sendMessageService = sendMessageService;
    }

    @RequestMapping("/sendMessage")
    public Result sendMessage(@RequestBody SendMessage sendMessage)
    {
        System.out.println(sendMessage );
        if (sendMessage==null||sendMessage.getUserId()<=0){
            return Result.error("-1","参数错误");
        }
        Boolean aBoolean = sendMessageService.sendMessage(sendMessage);
        return Result.success(aBoolean);
    }

    @RequestMapping("/getMsgData")
    public Result getMsgData(int userId)
    {
        if (userId<=0){
            return Result.error("-1","参数错误");
        }
        ArrayList<SendMessage> msgData = sendMessageService.getMsgData(userId);
        return Result.success(msgData);
    }

    @RequestMapping("/imRead")
    public Result imRead(int id){
        if (id<=0){
            return Result.error("-1","参数错误");
        }
        Boolean aBoolean = sendMessageService.imRead(id);
        if (aBoolean){
            return Result.success();
        }else {
            return Result.error("-1","未知错误");
        }
    }
}
